﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Commerce;

namespace Kooboo.Commerce.Promotions.Cashback.Controllers
{
     [Authorize(Roles = "Administrator")]
    public class CashbackController : Controller
    {
        //
        // GET: /Cashback/

        public ActionResult Index(int id)
        {
            var rule = Services.Current.DataContext.Promotion_Cashbacks.Where(i => i.PromotionRuleId == id).FirstOrDefault();
            if (rule == null)
            {
                rule = new Promotion_Cashback
                {
                    PromotionRuleId = id
                };
            }
            return View(rule);
        }

        [HttpPost]
        public ActionResult Index(Promotion_Cashback settings,string next)
        {
            var context = Services.Current;
            var item = context.DataContext.Promotion_Cashbacks.Where(i => i.PromotionRuleId == settings.PromotionRuleId).FirstOrDefault();
            if (item == null)
            {
                item = new Promotion_Cashback();
                item.PromotionRuleId = settings.PromotionRuleId;
                context.DataContext.Promotion_Cashbacks.InsertOnSubmit(item);
            }

            item.FromTotal = settings.FromTotal;
            item.ToTotal = settings.ToTotal;
            item.ReturnAmount = settings.ReturnAmount;

            context.DataContext.SubmitChanges();

            string url = string.Empty;
            if (next == "prev")
            {
                url = this.Url.Action("Edit", "PromotionRule", new { Id = settings.PromotionRuleId });
            }
            else
            {
                url = this.Url.Action("Index", "PromotionRule");
            }

            return Json(new { Data = url });
        }

    }
}
